package com.yq.cp.service.permission.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.config.MyException;
import com.yq.cp.entity.common.ErrorCode;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.Role;
import com.yq.cp.entity.permission.RolePermission;
import com.yq.cp.entity.permission.dto.RoleDto;
import com.yq.cp.entity.permission.vo.PermissionVo;
import com.yq.cp.entity.permission.vo.RoleVo;
import com.yq.cp.mapper.permission.RoleMapper;
import com.yq.cp.mapper.permission.RolePermissionMapper;
import com.yq.cp.service.permission.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author zh
 * @since 2022-09-21
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    RoleMapper roleMapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;

    @Override
    public void add(RoleVo vo) {
        Role role=new Role();
        role.setName(vo.getName());

        role.setType(11);

        role.setCreateUserId(vo.getCreateUserId());
        //role.setCompanyId(0L);
        if (isExist(role)){
            throw new MyException(ErrorCode.role_1202);
        }
        roleMapper.insert(role);

        //录入角色权限
//        if (vo.getList()!=null && vo.getList().size()>0){
//            for (PermissionVo permissionVo : vo.getList()) {
////                this.addPer(role.getId(),permissionVo);
//                RolePermission rolePermission=new RolePermission();
//                rolePermission.setRoleId(role.getId());
//                rolePermission.setPermissionId(Long.valueOf(permissionVo.getId()));
//                rolePermissionMapper.insert(rolePermission);
//                if (permissionVo.getChildren()!=null && permissionVo.getChildren().size()>0){
//                    for (PermissionVo p:permissionVo.getChildren()){
//                        RolePermission rolePermission1=new RolePermission();
//                        rolePermission1.setRoleId(role.getId());
//                        rolePermission1.setPermissionId(p.getId());
//                        rolePermissionMapper.insert(rolePermission1);
//                    }
//                }
//            }
//        }
        String[] split = vo.getIds().split(",");
        if (vo.getIds()!=null && split.length>0){
            for (String id : split) {
                RolePermission rolePermission=new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermissionId(Long.valueOf(id));
                rolePermissionMapper.insert(rolePermission);
            }
        }
    }

    @Override
    public void update(RoleVo vo) {
        Role role=getById(vo.getId());
        role.setName(vo.getName());
        if (isExist(role)){
            throw new MyException(ErrorCode.role_1202);
        }
        roleMapper.updateById(role);
        //删除所有权限
        rolePermissionMapper.deleteByRoleId(role.getId());
        //录入角色权限
//        if (vo.getList()!=null && vo.getList().size()>0){
//            for (PermissionVo permissionVo : vo.getList()) {
//                this.addPer(role.getId(),permissionVo);
//            }
//        }
        String[] split = vo.getIds().split(",");
        if (vo.getIds()!=null && split.length>0){
            for (String id : split) {
                RolePermission rolePermission=new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermissionId(Long.valueOf(id));
                rolePermissionMapper.insert(rolePermission);
            }
        }
    }

    @Override
    public void delete(RoleVo vo) {
        Role role=getById(vo.getId());
        if (role.getAllowDelete()!=null && role.getAllowDelete()==1){
            throw new MyException("该角色不允许删除");
        }
        //删除所有权限
        rolePermissionMapper.deleteByRoleId(role.getId());
        //删除角色
        roleMapper.deleteById(role.getId());
    }


    //递归录入权限
    private void addPer(Long roleId, PermissionVo permissionVo){
        RolePermission rolePermission=new RolePermission();
        rolePermission.setRoleId(roleId);
        rolePermission.setPermissionId(permissionVo.getId());
        rolePermissionMapper.insert(rolePermission);
        if (permissionVo.getChildren()!=null && permissionVo.getChildren().size()>0){
            for (PermissionVo p : permissionVo.getChildren()) {
                this.addPer(roleId,p);
            }
        }
    }

    @Override
    public IPage<Role> page(PageVo pageVo) {
        IPage<Role> iPage = new Page<>(pageVo.getPage(), pageVo.getSize());

        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        if (pageVo.getCompanyId()!=null){
            queryWrapper.eq("company_id",pageVo.getCompanyId());
        }
        queryWrapper.notIn("id",10);
        iPage=roleMapper.selectPage(iPage,queryWrapper);

        return iPage;
    }



    @Override
    public IPage<RoleDto> pageAndUserNumber(RoleVo pageVo) {
        IPage<RoleDto> iPage = new Page<>(pageVo.getPage(), pageVo.getSize());
        IPage<RoleDto> roleDtoIPage = roleMapper.pageAndUserNumber(iPage, pageVo);

        return iPage;
    }



    /**判断角色名称是否重复*/
    private boolean isExist(Role role){
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",role.getName());
        if (role.getId()!=null){
            queryWrapper.notIn("id",role.getId());
        }
        long count = roleMapper.selectCount(queryWrapper);
        if (count>0){
            return true;
        }
        return false;

    }
}
